package cn.rokhdelar.ams.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysRole;
import cn.rokhdelar.ams.management.domain.po.SysRoleAuthority;
import cn.rokhdelar.ams.management.domain.query.RoleQuery;
import cn.rokhdelar.ams.management.domain.vo.AuthorityVO;
import cn.rokhdelar.ams.management.domain.vo.RoleVO;
import cn.rokhdelar.ams.management.mapper.SysRoleMapper;
import cn.rokhdelar.ams.management.service.SysRoleAuthorityService;
import cn.rokhdelar.ams.management.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author rokhdelar
* 针对表【sys_role(角色表)】的数据库操作Service实现
* @since 2024-10-02 13:56:45
*/
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService {
    private final SysRoleAuthorityService roleAuthorityService;

    /**
     * @param query 查询条件实体
     * @return 角色信息列表和总数分页信息
     */
    @Override
    @Transactional
    public PageVO<RoleVO> getRoleListByPage(RoleQuery query) {
        Page<SysRole> page = query.toPageDefaultSortByCreateTime();

        Page<SysRole> rolePage = lambdaQuery()
                .like(query.getName() != null, SysRole::getName, query.getName())
                .eq(query.getStatus() != null, SysRole::getStatus, query.getStatus())
                .page(page);

        return PageVO.of(rolePage, RoleVO.class);
    }

    /**
     * @param id 角色ID
     * @return 角色详情信息
     */
    @Override
    @Transactional
    public RoleVO getRoleById(String id) {
        SysRole sysRole = getById(id);
        RoleVO roleVO = BeanUtil.copyProperties(sysRole, RoleVO.class);
        roleVO.setAuthorityList(baseMapper.getAuthoritiesByRoleId(id));
        return roleVO;
    }

    @Override
    @Transactional
    public Boolean updateRole(RoleVO role) {
        //  更新基础信息
        SysRole sysRole = BeanUtil.copyProperties(role, SysRole.class);
        if (saveOrUpdate(sysRole)) {
            //  基本信息更新成功，更新权限信息
            List<AuthorityVO> authorityList = role.getAuthorityList();

            LambdaQueryWrapper<SysRoleAuthority> queryWrapper = new LambdaQueryWrapper<SysRoleAuthority>()
                    .eq(SysRoleAuthority::getRoleId, role.getId());
            //  删除原有权限信息
            boolean remove = roleAuthorityService.remove(queryWrapper);
            //  插入新的权限信息
            for (AuthorityVO authorityVO : authorityList) {
                SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
                sysRoleAuthority.setRoleId(role.getId());
                sysRoleAuthority.setAuthorityId(authorityVO.getId());
                roleAuthorityService.save(sysRoleAuthority);
            }
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean createRole(RoleVO role) {
        //  更新基础信息
        SysRole sysRole = BeanUtil.copyProperties(role, SysRole.class);
        if (saveOrUpdate(sysRole)) {
            //  基本信息更新成功，更新权限信息
            List<AuthorityVO> authorityList = role.getAuthorityList();


            //  插入新的权限信息
            for (AuthorityVO authorityVO : authorityList) {
                SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
                sysRoleAuthority.setRoleId(sysRole.getId());
                sysRoleAuthority.setAuthorityId(authorityVO.getId());
                roleAuthorityService.save(sysRoleAuthority);
            }
        }
        return true;
    }
}




